﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="WebRole.cs" company="Spenta Consulting">
//   (c) Copyright 2011
// </copyright>
// <summary>
//   When running on Azure, this class is instantiated the first time the application is loaded.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace Spenta.StreetCare.MVC.Azure
{
    using System.Linq;
    using Microsoft.WindowsAzure.ServiceRuntime;

    /// <summary>
    /// When running on Azure, this class is instantiated the first time the application is loaded.
    /// </summary>
    public class WebRole : RoleEntryPoint
    {
        /// <summary>
        /// Called by Windows Azure to initialize the role instance.
        /// </summary>
        /// <returns>
        /// True if initialization succeeds, False if it fails. The default implementation returns True.
        /// </returns>
        public override bool OnStart()
        {
            // For information on handling configuration changes
            // see the MSDN topic at http://go.microsoft.com/fwlink/?LinkId=166357.
            RoleEnvironment.Changing += RoleEnvironmentChanging;

            return base.OnStart();
        }

        /// <summary>
        /// Roles the environment changing.
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="e">The <see cref="Microsoft.WindowsAzure.ServiceRuntime.RoleEnvironmentChangingEventArgs"/> instance containing the event data.</param>
        private static void RoleEnvironmentChanging(object sender, RoleEnvironmentChangingEventArgs e)
        {
            // for any configuration setting change except EnableTableStorageTraceListener
            if (e.Changes.OfType<RoleEnvironmentConfigurationSettingChange>().Any(change => change.ConfigurationSettingName != "EnableTableStorageTraceListener"))
            {
                // Set e.Cancel to true to restart this role instance
                e.Cancel = true;
            }
        }
    }
}